package com.bana.fvs.task;

import com.bana.fvs.service.ControllerService;
import com.bana.fvs.service.FogAreaService;
import com.bana.fvs.code.ParameterKeys;
import com.bana.sys.service.ParameterService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 更新缓存
 */
@Configuration
public class CacheTask {

    @Resource
    private FogAreaService fogAreaService;

    @Resource
    private ControllerService controllerService;

    @Resource
    private ParameterService parameterService;

    /**
     * 当前刷轮询的的分钟数
     */
    private int currentMinute =0;

    @Scheduled(fixedDelay=60*1000,initialDelay=10*1000)
    public void handle(){
        int getLampsSpan =parameterService.getInteger(ParameterKeys.RefreshCache);
        if(getLampsSpan<=0)
        {
            getLampsSpan=3;
        }
        int switchTimeout =parameterService.getInteger(ParameterKeys.SwitchTimeout);
        int forceMinute =parameterService.getInteger(ParameterKeys.ForceMinute);

        //更新雾区缓存
        fogAreaService.refreshFogAreaCaches();
        //检查光感
        fogAreaService.calculateFogAreasLight(currentMinute);
        //强制下发
        fogAreaService.syncFogAreas(forceMinute>0&&currentMinute%forceMinute==0);

        if(currentMinute % getLampsSpan ==0)
        {
            //更新控制器缓存
            controllerService.queryLampCaches(switchTimeout);
        }
        currentMinute+=1;

    }

}
